﻿using System;
using System.Collections.Generic;
using System.Text;
using IService.Data.IManager;
using System.Data;
using IService.DataDLL;
using IService.Model;

namespace IService.Data.OlbImpl.ManagerImpl
{
	public class UserDataManagerImpl : IUserDataManager
	{
		public User Login(string username, string userpass)
		{
			User currentuser = new User();
			string whereclause = "UserName = '" + username + "' and Password='" + userpass + "'";
			if (currentuser.Select(whereclause))
			{
				return currentuser;
			}
			return null;
		}

		public User GetUserByUserName(User user, string WhereClause)
		{
			try
			{
				user.Select(WhereClause);
				return user;
			}
			catch (Exception e)
			{
				return user;
			}
		}

		public User GetUserByEmail(User user, string WhereClause)
		{
			try
			{
				user.Select(WhereClause);
				return user;
			}
			catch (Exception e)
			{
				return user;
			}
		}

		public User GetUserById(User user)
		{
			try
			{
				user.Select();
				return user;
			}
			catch (Exception e)
			{
				return user;
			}
		}

		public DataTable GetUserListPagin(Type infoType, PagingEntity pe)
		{
			return DataAccess.SelectPaging(infoType, pe);
		}

		public DataTable GetUserByType(Type infoType, string WhereClause, string Order)
		{
			return DataAccess.Select(infoType, WhereClause, Order);
		}

		public DataTable GetLatestUserList(Type infoType, int usernum)
		{
			string WhereClause = "[ShowWebsite]=true";
			string Order = "[CreateTime] desc";
			return DataAccess.SelectTop(infoType, usernum, WhereClause, Order);
		}

		//获取产品列表，以年份排序倒叙
		public List<User> GetUserList(Type infoType)
		{
			DataTable result = DataAccess.Select(infoType, null, "PublishTime desc");
			return CollectionHelper.ConvertTo<User>(result);
		}

		//通过年份获取产品
		public DataTable GetUserByYear(Type infoType, int year)
		{
			string WhereClause = string.Format("[ShowWebsite]=true and year([PublishTime])={0}", year);
			string Order = "[PublishTime] desc";
			return DataAccess.Select(infoType, WhereClause, Order);
		}

		/// <summary>
		/// 更新
		/// </summary>
		/// <param name="type"></param>
		/// <returns></returns>
		public bool InsertUser(User user)
		{
			return DataAccess.Insert(user);
		}

		public bool UpdateUser(User user)
		{
			return DataAccess.Update(user);
		}

		public bool DeleteUser(User user)
		{
			return DataAccess.Delete(user);
		}

		public bool DeleteUserByIds(Type infoType, string WhereClause)
		{
			return DataAccess.Delete(infoType, WhereClause);
		}
	}
}
